package com.yike.user.strategy.login.type;

import com.yike.common.exception.BusinessException;
import com.yike.user.entity.User;
import com.yike.user.param.LoginParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Sakura
 * @date 2024/6/17 14:30
 */
@Service
public class LoginTypeContext {

    private final Map<Integer, LoginTypeStrategy> strategyMap = new HashMap<>();

    @Autowired
    private AccountLoginStrategy accountLoginStrategy;

    @Autowired
    private WxAppLoginStrategy wxAppLoginStrategy;

    @Autowired
    private SmsLoginStrategy smsLoginStrategy;

    @PostConstruct
    public void init() {
        strategyMap.put(1, accountLoginStrategy);
        strategyMap.put(2, wxAppLoginStrategy);
        strategyMap.put(3, smsLoginStrategy);
    }

    public User login(LoginParam loginParam) {
        LoginTypeStrategy strategy = strategyMap.get(loginParam.getLoginType());
        if (strategy != null) {
            return strategy.login(loginParam);
        } else {
            throw new BusinessException(500, "无效的登录类型: " + loginParam.getLoginType());
        }
    }
}
